'use strict';

angular.module('core')

.controller('HomeController', ['$scope', '$filter', 'homeService','PublicEnum','PublicFunction','PublicModule',
    function($scope, $filter, homeService, PublicEnum, PublicFunction, PublicModule) {
        // PublicFunction.loading(false);
        $scope.date = new Date;
        $scope.DepartmentId = '';
        //四个模块显示的标志位
        $scope.CompleteManageShow = true;
        $scope.QualityControlShow = false;
        $scope.StatisticsShow = false;
        $scope.ProductionReportShow = false;
        $scope.ChartShow = false;
        $scope.ChartRightShow = false;

        $scope.charts = {
            pieConfig: { notMerge: true }
        };

        var year = $scope.date.getFullYear()+'';
        var month = ($scope.date.getMonth()+1)+'';
        var day = $scope.date.getDate()+'';
        if(month.length==1){
            month = "0"+month;
        }
        if(day.length==1){
            day = "0"+day;
        } 

        PublicModule.initModule($scope, '_CompletePlan');
        $scope.Module._CompletePlan.LoadMode = PublicEnum.LoadMode.CacheAndPaging;
        $scope.Module._CompletePlan.PageSize = 15;
        $scope.toCompleteManage = function(){
            $scope.CompleteManageShow = true;
            $scope.QualityControlShow = false;
            $scope.StatisticsShow = false;
            $scope.ProductionReportShow = false;
            $scope.ChartShow = false;
            $scope.ChartRightShow = false;
            
        }; 
        $scope.toQualityControl = function(){
            $scope.CompleteManageShow = false;
            $scope.QualityControlShow = true;
            $scope.StatisticsShow = false;
            $scope.ProductionReportShow = false;
            if($scope.charts.pieOption===null&&$scope.charts.barOption===null){
              $scope.ChartShow = false; 
              $scope.ChartRightShow = false;
            }
            else{
              $scope.ChartShow = true;
              if($scope.charts.barOption===null){
                 $scope.ChartRightShow = false;
              }
              else{
                 $scope.ChartRightShow = true;
              }
               
            }
            
            PublicModule.initModule($scope, 'Department');
            $scope.Module.Department.LoadMode = PublicEnum.LoadMode.CacheAndPaging;
            $scope.Module.Department.PageSize = 15;
            PublicModule.initModule($scope, 'WorkLocation');
            $scope.Module.WorkLocation.LoadMode = PublicEnum.LoadMode.CacheAndPaging;
            $scope.Module.WorkLocation.PageSize = 15;
    
            var department = angular.fromJson(window.localStorage.getItem("DepartmentFocusedRecord"));
            var workLocation = angular.fromJson(window.localStorage.getItem("WorkLocationFocusedRecord"));
            if(department){         
                $scope.Module.Department.FocusedRecord = department;
            }
            if(workLocation){         
                $scope.Module.WorkLocation.FocusedRecord = workLocation;
            }
            

        }; 
        $scope.beforeshow = function (refScope) {
            refScope.Condition.Department = $scope.Module.Department.FocusedRecord._id;
        };
        $scope.toStatistics = function(){
            $scope.CompleteManageShow = false;
            $scope.QualityControlShow = false;
            $scope.StatisticsShow = true;
            $scope.ProductionReportShow = false;
            $scope.ChartShow = false;
            $scope.ChartRightShow = false;
        };
        $scope.toProductionReport = function(){
            $scope.CompleteManageShow = false;
            $scope.QualityControlShow = false;
            $scope.StatisticsShow = false;
            $scope.ProductionReportShow = true;
            $scope.ChartShow = false;
            $scope.ChartRightShow = false;
        };

        $scope.getItemWidth = function (item) {
            var width = item*100;
            return width;
        };
        $scope.onDownload = function (item) {
            var width = item;
            return width;
        };

        $scope.turnPage = function (records) {
            var elements = angular.element('.rate-blue');
            for (var i = 0; i < records.length; i++) {
                var rate = records[i].QtyRate;
                
                elements[i+1].style.width = rate+"%";
                elements[i+1].innerHTML = rate.toFixed(2)+"%";
            };


        };
        
        init();

        function init () {
            getCompletePlanData(false);
        }
           
        function getCompletePlanData (loading) {          
            if(loading){
               PublicFunction.loading(true); 
            } 
            homeService.getCompletePlanByDate(year+"-"+month+"-"+day).then(function (result) {
                if(loading){
                   PublicFunction.loading(false); 
                }
                $scope.Module._CompletePlan.Records = result; 
                $scope.Module._CompletePlan.emitOnLoadEvent();
                // var elements = angular.element('.rate-blue');
                // var nums = 0;
                
                // for (var i = 0; i < $scope.Module._CompletePlan.Records.length; i++) {
                //     var rate = $scope.Module._CompletePlan.Records[i].QtyRate;
                    
                //     elements[i+1].style.width = rate+"%";
                //     elements[i+1].innerHTML = rate.toFixed(2)+"%";
                // };
            },function(error){
                if(error){
                   PublicFunction.alert(error.message, error.title); 
                }
                
            });
        }
         
        

        function getDateData (){
            year = $scope.date.getFullYear()+'';
            month = ($scope.date.getMonth()+1)+'';
            day = $scope.date.getDate()+'';
            if(month.length==1){
                month = "0"+month;
            }
            if(day.length==1){
                day = "0"+day;
            }  
        }

        //前一天
        $scope.beforeDay = function () {
            var t=$scope.date.getTime()-1000*60*60*24;
            $scope.date=new Date(t);
            getDateData();
            getCompletePlanData(true);
        };

        //后一天
        $scope.afterDay = function () {
            var t=$scope.date.getTime()+1000*60*60*24;
            $scope.date=new Date(t);
            getDateData();
            getCompletePlanData(true);
        };

        //刷新
        $scope.refresh = function () {
            window.localStorage.setItem("WorkLocationFocusedRecord", angular.toJson($scope.Module.WorkLocation.FocusedRecord));
            window.localStorage.setItem("DepartmentFocusedRecord", angular.toJson($scope.Module.Department.FocusedRecord));
            homeService.getPieChartData(year+"-"+month+"-"+day,$scope.Module.WorkLocation.FocusedRecord._id).then(function (result) {
                $scope.ChartShow = true;
                $scope.charts.pieOption = homeService.getChartOption(result);
                if(result.itemBadTypes.length>0){
                    $scope.ChartRightShow = true;
                    $scope.charts.barOption = homeService.getBarChartOption(result.itemBadTypes);
                }else{
                    $scope.charts.barOption = null;
                    $scope.ChartRightShow = false;
                }
                
                
            },function(error){
                $scope.charts.pieOption = null;
                $scope.charts.barOption = null;
                $scope.ChartShow = false;
                if(error){
                   PublicFunction.alert(error.message, error.title); 
                }
                
            });
            
            // homeService.getPieChartData('2016-09-07','57ce7a2e01cb5c0b9490e259').then(function (result) {
            //     $scope.charts.pieOption = homeService.getChartOption(result);
            //     $scope.charts.barOption = homeService.getBarChartOption(result,year+"-"+month+"-"+day);
            // },function(error){
            //     PublicFunction.alert(error.message, error.title);
            // });
        };

    }
]);